package com.lanou3g.k12.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.Orders;
import com.lanou3g.k12.vo.OrdersFind;
import com.lanou3g.k12.service.OrdersService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

@RestController
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 基础查询
     *
     * @param id 订单号
     * @return 状态码
     */
    @GetMapping("/orders/{id}")
    public K12Result searchById(@PathVariable Integer id) {
        try {
            Orders orders = ordersService.findById(id);
            System.out.println(id);
            return K12ResultUtils.ok(orders);
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    /**
     * 多条件查询
     *
     * @param page       分页数
     * @param ordersFind 参数对象
     * @param size       每页数据量
     * @return 状态码
     */
    @GetMapping("/orders")
    public K12Result search(@RequestParam(value = "page", defaultValue = "1") Integer page,
                            OrdersFind ordersFind,
                            @RequestParam(value = "size", defaultValue = "10") Integer size) {

        Page<Object> objects = PageHelper.startPage(page, size);
        try {
            ordersService.find(ordersFind);
            return K12ResultUtils.ok(objects);
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }

    }

    /**
     * 时间区间查询
     *
     * @param date  时间1
     * @param date1 时间2
     * @return
     */
    @GetMapping("/orders/order_date/{date}/{date1}")
    public K12Result findByDate(@PathVariable long date, @PathVariable long date1) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String format = df.format(date);
        String format1 = df.format(date1);
        System.out.println(format);
        System.out.println(format1);

        List<Orders> byDate = ordersService.findByDate(format, format1);
        return K12ResultUtils.ok(byDate);
    }

    /**
     * 联表查询
     *
     * @param cid 课程表id
     * @return 状态码
     */
    @GetMapping("/orders/course/{cid}")
    public K12Result findByCid(@PathVariable int cid) {
        List<Orders> byCid = ordersService.findByCid(cid);
        return K12ResultUtils.ok(byCid);
    }

    /**
     * 查询我的订单
     *
     * @param uid    用户id
     * @param page   分页数
     * @param size   每页数据量
     * @param status 支付状态
     * @return
     */
    @GetMapping("/findOrders/{uid}")
    public K12Result findOrders(@PathVariable Long uid,
                                @RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "size", defaultValue = "30") Integer size,
                                Integer status) {
        try {
            Page<Object> objects = PageHelper.startPage(page, size);
            ordersService.findOrders(uid, status);
            return K12ResultUtils.ok(objects);
        } catch (Exception e) {
            return K12ResultUtils.error(533);
        }

    }

    /**
     * 订单表插入一条数据，中间表多一条cid，oid
     *
     * @param orders 订单对象
     * @param csId   课程id
     * @return
     */
    @PostMapping("/order")
    public K12Result insert(Orders orders, Long csId) {

        try {
            ordersService.add(orders, csId);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }


}
